package wk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import wk.entity.Admin;
import wk.service.impl.AdminServiceImpl;
import wk.utils.md5.MD5Code;
import wk.utils.validation.Validater;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@Controller
@RequestMapping("user")
public class LoginController {
    @Autowired
    private AdminServiceImpl adminService;

    @RequestMapping("login")
    public void login(Admin vo, HttpServletResponse response, HttpServletRequest request) throws Exception {
        if (Validater.validationEmpty(vo.getAdminName()) || Validater.validationEmpty(vo.getPassWord())) {
            vo.setIp(request.getRemoteAddr()); // 取得客户端ip
            System.out.println("--------"+vo.getIp());
            vo.setAdminName(vo.getAdminName());
            vo.setPassWord(new MD5Code().getMD5ofStr("{" + vo.getPassWord() + "}"));
            Admin admin = adminService.login(vo);
            if (admin != null) { // 登录成功
                request.getSession().setAttribute("admin", admin); // 保存的数据包括：tid, flag, lastdate, photo, name,cid
                response.getWriter().print("success");
            } else {
                response.getWriter().print("failure");
            }
        } else {
            response.getWriter().print("null-error");
        }
    }

    @RequestMapping("register")
    public void regiser(@RequestBody Map<String,String> map, HttpServletResponse response) throws IOException, SQLException {
        if (Validater.validationEmpty(map.get("adminname")) || Validater.validationEmpty(map.get("password")) ||
                Validater.validationEmpty(map.get("repass"))) {
            if (map.get("password").equals(map.get("repass"))){
                Admin ad=new Admin();
                ad.setAdminName(map.get("adminname"));
                ad.setPassWord(new MD5Code().getMD5ofStr("{" + map.get("password") + "}"));
                ad.setCellPhone(map.get("cellphone"));
                ad.setNickName(map.get("nickname"));
                if(adminService.insertadmin(ad)==1){
                    response.getWriter().print("success");
                }else {
                    response.getWriter().print("failure");
                }
            }else{
                response.getWriter().print("null-error");
            }
        }

    }

    @RequestMapping("getadmincount")
    public void getadmincount(@RequestBody Map<String,String> map,HttpServletResponse response) throws SQLException, IOException {
        String adminname=map.get("adminname");
        if(adminService.getCountByadminname(adminname)==1){
            response.getWriter().print("failure");
        }
    }

    @RequestMapping("foundPWDByTel")
    public void foundPWDByTel(){


    }


    @RequestMapping("aliyunSms")
    public void aliyunSms(){

    }


}
